package yawning.mew.bear;

import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationEventAction;

public class FrenziedRegenerationAction extends SimulationEventAction {
	BearThreatSimulator mState;
	double mCooldownExpireTime;
	boolean mHasGlyph;

	FrenziedRegenerationAction(BearThreatSimulator state) {
		super(state.mSimEngine);
		mState = state;

		mHasGlyph = mState.mModel.mToon.hasGlyph(BearModel.GLYPH_FRENZIEDREGENERATION);
	}

	@Override
	public double perform() {
		if (isUp() || mCooldownExpireTime > mState.mSimEngine.getSimTime()) return SimulationAction.ACTION_FAIL;
		if (mState.mIsLogging) mState.mLog.log("Frenzied Regeneration\n");

		mState.mModel.multiplyMaxHP(mState.mModel.multFrenziedRegenerationMaxHP);

		if (!mHasGlyph)
			schedule(mState.mModel.frenziedRegenerationBuffDuration, 1.0);
		else
			schedule(mState.mModel.frenziedRegenerationBuffDuration);

		mCooldownExpireTime = mState.mSimEngine.getSimTime() + mState.mModel.frenziedRegenerationCDDuration;

		return 0.0;
	}

	@Override
	public void onExpire() {
		mState.mModel.multiplyMaxHP(1.0f / mState.mModel.multFrenziedRegenerationMaxHP);

		if (mState.mIsLogging) mState.mLog.log("Frenzied Regeneration Expiration\n");
	}

	@Override
	public void onTick() {
		if (!mHasGlyph && (mState.mUnlimitedRage || mState.mRage > 0)) {
			double heal = mState.mModel.getHealth() * mState.mModel.multFrenziedRegenerationHeal;
			int rageUsed = mState.mUnlimitedRage ? 10 : (int) Math.floor(Math.min(10, mState.mRage));

			heal *= rageUsed;

			if (mState.mIsLogging) mState.mLog.log("Frenzied Regeneration: ");

			if (mState.mSimEngine.getRandom().nextBoolean(mState.mModel.getHealCritChance())) {
				heal *= mState.mModel.multHealCrit;
				if (mState.mIsLogging) mState.mLog.logAppend("CRIT ");
			} else {
				if (mState.mIsLogging) mState.mLog.logAppend("HIT ");
			}

			if (mState.mIsLogging) mState.mLog.logAppend("Heal: %d\n", Math.round(heal));

			mFrenziedRegenerationHeal += Math.round(heal);

			mState.onRageLoss(rageUsed, false);
			mNrHeals++;
		}
	}

	public double getMult() {
		if (isUp() && mHasGlyph) return mState.mModel.multFrenziedRegenerationGlyphIncHeal;

		return 1.0;
	}

	int mNrHeals;
	long mFrenziedRegenerationHeal;
}